package com.tianxiao.faas.biz.service.corner;

import com.tianxiao.faas.api.param.corner.*;
import com.tianxiao.faas.common.constants.CommonConstants;
import com.tianxiao.faas.common.exception.biz.BizException;
import com.tianxiao.faas.common.util.PageUtils;
import com.tianxiao.faas.mapper.dao.OrderHistoryModelMapper;
import com.tianxiao.faas.mapper.dao.ShopModelMapper;
import com.tianxiao.faas.mapper.model.OrderHistoryModel;
import com.tianxiao.faas.mapper.model.ShopModel;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AiHistoryService {

    @Resource
    private OrderHistoryModelMapper orderHistoryModelMapper;

    @Resource
    private ShopModelMapper shopModelMapper;

    public List<OrderHistoryResult> orderHistory(OrderHistoryParam param, Long userId) {
        List<OrderHistoryModel> orderHistoryModels = new ArrayList<>();
        if (param.getShopId() == null) {
            orderHistoryModels.addAll(orderHistoryModelMapper.list(userId, CommonConstants.HISTORY_ORDER, PageUtils.getOffset(param.getPage(), param.getPageSize()), param.getPageSize()));
        } else {
            orderHistoryModels.addAll(orderHistoryModelMapper.orderHistoryByShopId(userId, param.getShopId(), CommonConstants.HISTORY_ORDER, PageUtils.getOffset(param.getPage(), param.getPageSize()), param.getPageSize()));
        }
        if (CollectionUtils.isEmpty(orderHistoryModels)) {
            return Collections.emptyList();
        }
        List<OrderHistoryResult> orderHistoryResults = new ArrayList<>();

        for (OrderHistoryModel orderHistoryModel : orderHistoryModels) {
            OrderHistoryResult orderHistoryResult = getOrderHistoryResult(orderHistoryModel);
            orderHistoryResults.add(orderHistoryResult);
        }
        return orderHistoryResults;
    }


    public Integer orderHistoryCount(OrderHistoryParam param, Long userId) {
        return orderHistoryModelMapper.countByShopId(userId, param.getShopId(), CommonConstants.HISTORY_ORDER);
    }

    private OrderHistoryResult getOrderHistoryResult(OrderHistoryModel orderHistoryModel) {
        OrderHistoryResult orderHistoryResult = new OrderHistoryResult();
        orderHistoryResult.setId(orderHistoryModel.getId());
        orderHistoryResult.setUserId(orderHistoryModel.getUserId());
        orderHistoryResult.setShopId(orderHistoryModel.getShopId());
        orderHistoryResult.setUserRequirement(orderHistoryModel.getUserRequirement());
        return orderHistoryResult;
    }

    public OrderHistoryDetailsResult orderDetails(Long id) {
        OrderHistoryModel orderHistoryModel = orderHistoryModelMapper.selectByPrimaryKey(id);
        if (orderHistoryModel == null) {
            throw new BizException("帮点记录不存在");
        }
        ShopModel shopModel = shopModelMapper.selectByPrimaryKey(orderHistoryModel.getShopId());
        if (shopModel == null) {
            throw new BizException("餐厅不存在");
        }
        OrderHistoryDetailsResult orderHistoryDetailsResult = new OrderHistoryDetailsResult();
        orderHistoryDetailsResult.setId(orderHistoryModel.getId());
        orderHistoryDetailsResult.setUserRequirement(orderHistoryModel.getUserRequirement());
        orderHistoryDetailsResult.setResult(orderHistoryModel.getResult());
        orderHistoryDetailsResult.setShopName(shopModel.getName());
        orderHistoryDetailsResult.setShopId(orderHistoryModel.getShopId());
        orderHistoryDetailsResult.setShopImgs(shopModel.getImages());
        orderHistoryDetailsResult.setLogo(shopModel.getLogo());
        orderHistoryDetailsResult.setShareId(orderHistoryModel.getShareId());
        return orderHistoryDetailsResult;
    }

    public List<ReportHistoryResult> reportHistory(BasePageParam param, Long userId) {
        List<OrderHistoryModel> orderHistoryModels = orderHistoryModelMapper.list(userId, CommonConstants.HISTORY_REPORT, PageUtils.getOffset(param.getPage(), param.getPageSize()), param.getPageSize());

        List<Long> shopIds = orderHistoryModels.stream().map(OrderHistoryModel::getShopId).collect(Collectors.toList());
        List<ShopModel> shopModels = shopModelMapper.queryByIds(shopIds);
        if (CollectionUtils.isEmpty(shopModels)) {
            throw new BizException("查询餐厅为空");
        }
        Map<Long, ShopModel> shopModelMap = shopModels.stream().collect(Collectors.toMap(ShopModel::getId, s -> s));

        List<ReportHistoryResult> reportHistoryResults = new ArrayList<>();
        for (OrderHistoryModel orderHistoryModel : orderHistoryModels) {
            ShopModel shopModel = shopModelMap.get(orderHistoryModel.getShopId());
            ReportHistoryResult reportHistoryResult = getReportHistoryResult(orderHistoryModel, shopModel);
            reportHistoryResults.add(reportHistoryResult);
        }
        return reportHistoryResults;
    }

    private ReportHistoryResult getReportHistoryResult(OrderHistoryModel orderHistoryModel, ShopModel shopModel) {
        if (shopModel == null) {
            return null;
        }
        ReportHistoryResult reportHistoryResult = new ReportHistoryResult();
        reportHistoryResult.setId(orderHistoryModel.getId());
        reportHistoryResult.setShopName(shopModel.getName());
        reportHistoryResult.setImages(shopModel.getImages());
        reportHistoryResult.setShopId(orderHistoryModel.getShopId());
        return reportHistoryResult;
    }

    public Integer reportHistoryCount(Long userId) {
        return orderHistoryModelMapper.count(userId, CommonConstants.HISTORY_REPORT);
    }
}
